﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataLib.Accounts.Bus;
using System.Data;

namespace Ewts.Storage
{
    public partial class WaitWeighList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!Page.IsPostBack)
                {
                    User curUser = new User();
                    curUser = (User)Session["UserInfo"];
                    //归属地
                    ListItem item = new ListItem();
                    item.Text = "--请选择--";
                    item.Value = "";
                    DataLib.BLL.City bllLib = new DataLib.BLL.City();
                    dropBelong.DataSource = bllLib.GetList(" flag1='01'");
                    dropBelong.DataTextField = "cityname";
                    dropBelong.DataValueField = "cityname";
                    dropBelong.DataBind();
                    dropBelong.Items.Insert(0, item);


                    dropGraphic.DataSource = bllLib.GetList(" flag1='02'");
                    dropGraphic.DataTextField = "cityname";
                    dropGraphic.DataValueField = "cityname";
                    dropGraphic.DataBind();
                    dropGraphic.Items.Insert(0, item);

                    if (curUser.UserType != "AD")
                    {
                        dropGraphic.Enabled = false;
                        //dropGraphic.SelectedItem.Text = curUser.CompanyCity;
                        dropGraphic.SelectedValue = curUser.CompanyCity;
                        //dropBelong.SelectedIndex = 0;
                    }
                  
                   // bindData();
                  

                }
            }
        }

        /// <summary>
        /// 根据处理地绑定数据
        /// </summary>
        /// <param name="city"></param>
        protected void bindData()
        {
            //string where = " (c.status=2 or (c.islocal=0 and c.status=1)) ";
            string bcity = "";
            string gcity = "";
            if (dropGraphic.SelectedItem.Text != "--请选择--")
                gcity = dropGraphic.SelectedItem.Text;
            else
                gcity = "";

            if (dropBelong.SelectedItem.Text != "--请选择--")
                bcity = dropBelong.SelectedItem.Text;
            else
                bcity = "";

            int cjostatus =2;
            
            string outwhere = " status=3 ";

            DataTable dt = new DataTable();
            DataLib.BLL.CJOItem bll = new DataLib.BLL.CJOItem();
            dt = bll.GetList_PROCJO(bcity, gcity, 1, cjostatus);
            dt.Merge(bll.GetList_PROCJO(bcity, gcity, 0, cjostatus));
            //rept.DataSource = dt;
            //rept.DataBind();


            if (gcity != "")
            {
                outwhere += " and graphicCity='" + gcity + "'";
            }

            if (bcity != "")
            {
                outwhere += " and belogarea='" + bcity + "'";
            }
            
            //reptMove.DataSource = new DataLib.BLL.OutStorage().GetList(outwhere);
            //reptMove.DataBind();
        }

        /// <summary>
        /// 显示车牌号
        /// </summary>
        /// <param name="cjoid"></param>
        /// <param name="flag">0只取第一个，加省略号，1取多个，</param>
        /// <returns></returns>
        protected string GetPlateNo(string srcid,int flag1,int flagsrc)
        {
            string plateNumInfo = ""; 
            DataLib.Model.VehicleArrange vehicleModel = new DataLib.Model.VehicleArrange();
            DataLib.BLL.VehicleArrange vBll = new DataLib.BLL.VehicleArrange();
            if (flagsrc == 0) //cjoid
                vehicleModel = vBll.GetModelByCjoid(int.Parse(srcid));
            else
                vehicleModel = vBll.GetModelByOutid(int.Parse(srcid));
            if (vehicleModel != null)
            {
                DataLib.BLL.PlateNumber plateBll = new DataLib.BLL.PlateNumber();
                DataLib.Model.PlateNumber plateModel = new DataLib.Model.PlateNumber();
                plateNumInfo = GetPlateStr(vehicleModel.platenumberid, vehicleModel.sealno,flag1);
            }
            return plateNumInfo;

        }

        /// <summary>
        /// 显示收货员名
        /// </summary>
        /// <param name="cjoid"></param>
        /// <param name="flag">0只取第一个，加省略号，1取多个，</param>
        /// <returns></returns>
        protected string GetVehiclor(string cjoid, int flag)
        {
            DataLib.Model.VehicleArrange vehicleModel = new DataLib.BLL.VehicleArrange().GetModelByCjoid(int.Parse(cjoid));
            string vehiclor = "";
            if (vehicleModel != null)
            {
                DataLib.BLL.Vehiclor vBll = new DataLib.BLL.Vehiclor();
                DataLib.Model.Vehiclor vModel = new DataLib.Model.Vehiclor();
                string[] brr = vehicleModel.vehiderid.Split(',');
                if (brr.Length > 0)
                {
                    vModel = vBll.GetModel(int.Parse(brr[0]));
                    vehiclor += vModel.name;

                    if (brr.Length > 1)
                    {
                        if (flag == 0)
                        {
                            vehiclor += "...";
                        }
                        else
                        {
                            for (int i = 1; i < brr.Length; i++)
                            {
                                if (brr[i] != null && brr[i].Trim() != "")
                                {
                                    vehiclor += ",";
                                    vModel = vBll.GetModel(int.Parse(brr[i]));
                                    vehiclor += vModel.name;

                                }
                            }
                        }
                    }
                }

            }
            return vehiclor;

        }

        /// <summary>
        /// 获取字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string GetSubString(string str,int len)
        {
            string strreturn = "";
            if (str.Length > len)
            {
                strreturn = str.Substring(0, len) + "...";
            }
            else
            {
                strreturn = str;
            }
            return strreturn;
        }


        /// <summary>
        /// 根据车牌ID字段及封单号字段返回字符串
        /// </summary>
        /// <param name="plateid"></param>
        /// <param name="sealno"></param>
        /// <param name="flag"></param>
        /// <returns></returns>
        protected string GetPlateStr(string plateid, string sealno,int flag)
        {
            string platestr = "";
            string[] brr = plateid.Split(',');
            string[] sarr = sealno.Split('@');
            DataLib.Model.PlateNumber plateModel = new DataLib.Model.PlateNumber();
            DataLib.BLL.PlateNumber plateBll = new DataLib.BLL.PlateNumber();
            if (brr.Length > 0)
            {
                plateModel = plateBll.GetModel(int.Parse(brr[0]));
                platestr += plateModel.plateno + "-" + plateModel.maxton;
                if (sarr.Length >= 1 && sarr[0] != null && sarr[0].IndexOf(":") > 0)
                {
                    platestr += "-（";
                    platestr += sarr[0].Split(':')[1];
                    platestr += "）";
                }

                if (brr.Length > 1)
                {
                    if (flag == 0)
                    {
                        platestr += "...";
                    }
                    else
                    {
                        for (int i = 1; i < brr.Length; i++)
                        {
                            if (brr[i] != null && brr[i].Trim() != "")
                            {
                                platestr += "\n";
                                plateModel = plateBll.GetModel(int.Parse(brr[i]));
                                platestr += plateModel.plateno + "-" + plateModel.maxton + "-（";
                                foreach (string strseal in sarr)
                                {
                                    if (strseal != null && strseal.IndexOf(":") > 0 && strseal.IndexOf(brr[i]) >= 0)
                                    {
                                        string[] arrseal = strseal.Split(':');
                                        if (brr[i] == arrseal[0] && arrseal.Length > 1)
                                        {
                                            platestr += arrseal[1];
                                            break;
                                        }
                                    }
                                }
                                platestr += "）";

                            }

                        }
                    }
                }
            }
            return platestr;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="flag">标识为1取名称，否则 取编码</param>
        /// <returns></returns>
        public string GetCumByOutID(object id,int flag)
        {
            int outid = int.Parse(id.ToString());
            DataLib.Model.OutStorage model = new DataLib.BLL.OutStorage().GetModel(outid);
            //DataLib.BLL.LoadNum bllload = new DataLib.BLL.LoadNum();
            
            //if ((model.belogarea == model.inDepart) && (model.graphicCity != model.OutDepart))
            //{
            //    if (model.StorageType == "原料库")
            //        cumname = bllload.GetCusm(model.VLoadNum, 1);
            //    else
            //        cumname = model.belogarea + model.OutDepart + "库";
            //}
            //else
            //{
            //    cumname = model.inDepart + "伟翔";
            //}

            //2015年5月25日 21:49:23 FIFA
            string cumname = "";
            if (model.VLoadNum.Substring(0, 2) == "86")
            {
                DataLib.Model.LoadNum lmodel = new DataLib.BLL.LoadNum().GetLoadID(model.VLoadNum);
                //if (lmodel.labname != null && lmodel.labname.ToString().Trim() != "")
                    cumname = lmodel.labname;
            }
            else
            {
                DataLib.BLL.Vload vload = new DataLib.BLL.Vload();
                DataLib.Model.Vload mvload = vload.GetModelByOutID((int)model.id);
                cumname = mvload.inscumname;
            }
           

            return cumname;
        }

        /// <summary>
        /// 获取本地收集或者异地收集状态
        /// </summary>
        /// <param name="flag"></param>
        /// <returns></returns>
        public string GetCollStatus(string flag)
        {
            if (flag == "True")
            {
                return "本地收集";
            }
            else
            {
                return "异地收集";
            }
        }

        protected void dropGraphic_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindData();
        }
    }
}